package com.adou.Controller;

import com.adou.common.controller.BaseController;
import com.adou.common.dto.FlowDirectionOutDto;
import com.adou.common.model.FlowDirection;
import com.adou.common.util.Const;
import com.adou.common.util.StringUtil;
import com.adou.service.FlowDirectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("flowDirection")
@Scope(value = "prototype")
public class FlowDirectionController extends BaseController {

    @Autowired
    private FlowDirectionService flowDirectionService;

    /**
     * 分页数据
     * @param pageNum
     * @param pageSize
     * @param o
     * @param find 模糊查询内容：流程名称、开始环节名称、结束环节名称、流向名称
     * @return
     */
    @GetMapping("pageList")
    private Map<String, Object> pageList(int pageNum, int pageSize, FlowDirection o,String find){
        pageNum = pageNum <= 0 ? this.pageNum : pageNum;
        pageSize = pageSize <= 0 ? this.pageSize : pageSize;
        if (o.getIfmid()==null || o.getIfmid()==0){
            error(101,"ifmid为空");
            return resultMap;
        }
        o.setCfdname(find);
        try {
            Map<String, Object> map = flowDirectionService.findByPage(pageNum,pageSize,o);
            if ((long) map.get("totalcount") > 0) {
                List<FlowDirection> flowDirectionList=(List<FlowDirection>)map.get("list");
                List<FlowDirectionOutDto> flowDirectionOutDtoList = flowDirectionList.stream().map(FlowDirectionOutDto::new).collect(Collectors.toList());
                map.put("list", flowDirectionOutDtoList);
                resultMap.put("data", map);
                resultMap.put("msg", Const.DATA_SUCCEED);
            } else {
                error(101, "数据为空");
            }
        }
        catch (Exception e){
            resultMap.put("result", Const.DATA_ERROR_112);
            resultMap.put("msg", Const.DATA_FAIL);
            logger.error(e.toString());
        }
        return resultMap;
    }


    /**
     * 添加流程指向
     * @param flowDirection
     * @return
     */
    @PostMapping("insert")
    private  Map<String, Object> insert(FlowDirection flowDirection){
        if(flowDirection.getIfmid()==null || flowDirection.getIfmid()==0){
            error(203,"ifmid为空");
            return resultMap;
        }
        if (flowDirection.getCfdname()==null){
            error(204,"流向名称为空");
            return resultMap;
        }
        if (flowDirection.getIstartftid()==null){
            error(205,"开始节点为空");
            return resultMap;
        }
        if (flowDirection.getIendftid()==null){
            error(206,"结束节点为空");
            return resultMap;
        }
        if(flowDirection.getIstartftid()==-1){
            FlowDirection f = new FlowDirection();
            f.setIstartftid(flowDirection.getIstartftid());
            f.setIfmid(flowDirection.getIfmid());
            List<FlowDirection> list = flowDirectionService.find(f);
            if(list.size()>0){
                error(207,"一个流程只能有一个开始节点");
                return resultMap;
            }
        }
        if(flowDirection.getbIsCondition()){
            if(StringUtil.isBlank(flowDirection.getcFieldName()) || StringUtil.isBlank(flowDirection.getcCompare())
                    || StringUtil.isBlank(flowDirection.getcValue())){
                error(207,"启用流转条件请设置第一比较域名、运算符和比较值！");
                return resultMap;
            }
        }
        if(flowDirection.getbIsAndOr()){
            if(StringUtil.isBlank(flowDirection.getcOFieldName()) || StringUtil.isBlank(flowDirection.getcOCompare())
                    || StringUtil.isBlank(flowDirection.getcOValue())){
                error(208,"启用多流转条件请设置第二比较域名、运算符和比较值！");
                return resultMap;
            }
        }
        try {
            int res = flowDirectionService.insert(flowDirection);
            if (res==0){
                resultMap.put("result",Const.ADD_ERROR_201);
                resultMap.put("msg",Const.SAVE_FAIL);
            }else {
                resultMap.put("msg", Const.SAVE_SUCCEED);
                resultMap.put("data",res);
            }
        }catch (Exception e){
            logger.error(e.toString(), e);
            resultMap.put("result", Const.ADD_ERROR_202);
            resultMap.put("msg", Const.SAVE_FAIL);
        }
        return resultMap;
    }

    /**
     * 修改流程指向
     * @param flowDirection
     * @return
     */
    @PostMapping("update")
    private  Map<String, Object> update(FlowDirection flowDirection){
        if(StringUtil.isBlank(flowDirection.getId())){
            error(213,"id为空");
            return resultMap;
        }
        if (flowDirection.getCfdname()==null){
            error(214,"流向名称为空");
            return resultMap;
        }
        if (flowDirection.getIstartftid()==null){
            error(215,"开始节点为空");
            return resultMap;
        }
        if (flowDirection.getIendftid()==null){
            error(216,"结束节点为空");
            return resultMap;
        }
        if(flowDirection.getIstartftid()==-1){
            FlowDirection f = new FlowDirection();
            f.setIstartftid(flowDirection.getIstartftid());
            f.setIfmid(flowDirection.getIfmid());
            List<FlowDirection> list = flowDirectionService.find(f);
            if(list.size()>0 && list.get(0).getId()!=flowDirection.getId()){
                error(207,"一个流程只能有一个开始节点");
                return resultMap;
            }
        }
        if(flowDirection.getbIsCondition()){
            if(StringUtil.isBlank(flowDirection.getcFieldName()) || StringUtil.isBlank(flowDirection.getcCompare())
                    || StringUtil.isBlank(flowDirection.getcValue())){
                error(217,"启用流转条件请设置第一比较域名、运算符和比较值！");
                return resultMap;
            }
        }
        if(flowDirection.getbIsAndOr()){
            if(StringUtil.isBlank(flowDirection.getcOFieldName()) || StringUtil.isBlank(flowDirection.getcOCompare())
                    || StringUtil.isBlank(flowDirection.getcOValue())){
                error(218,"启用多流转条件请设置第二比较域名、运算符和比较值！");
                return resultMap;
            }
        }
        try {
            int res = flowDirectionService.update(flowDirection);
            if (res==0){
                resultMap.put("result",Const.UPDATE_ERROR_211);
                resultMap.put("msg",Const.UPDATE_FAIL);
            }else {
                resultMap.put("msg", Const.UPDATE_SUCCEED);
                resultMap.put("data",res);
            }
        }catch (Exception e){
            logger.error(e.toString(), e);
            resultMap.put("result", Const.UPDATE_ERROR_212);
            resultMap.put("msg", Const.UPDATE_FAIL);
        }
        return resultMap;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("delete")
    private  Map<String, Object> delete(String ids){
        if(!StringUtil.isInteger(ids.replaceAll(",",""))){
            error(303,"ids格式错误");
            return resultMap;
        }
        try {
            List<FlowDirection> list = new ArrayList<>();
            for (String id: ids.split(",")) {
                FlowDirection flowDirection = new FlowDirection();
                flowDirection.setId(Long.parseLong(id));
                list.add(flowDirection);
            }
            int res = flowDirectionService.deleteBatch(list);
            if (res==0){
                resultMap.put("result",Const.DEL_ERROR_301);
                resultMap.put("msg",Const.DEL_FAIL);
            }
            else if(res==-1){
                error(303,"流程运行主表已包含数据，无法删除");
            }
            else {
                resultMap.put("msg", Const.DEL_SUCCEED);
                resultMap.put("data",res);
            }
        }catch (Exception e){
            logger.error(e.toString(), e);
            resultMap.put("result", Const.DEL_ERROR_302);
            resultMap.put("msg", Const.DEL_FAIL);
        }
        return resultMap;
    }

    /**
     * 查询数据
     * @param ifmid 流程主键
     * @param istartftid 开始节点主键
     * @return
     */
    @GetMapping("find")
    private  Map<String, Object> find(Long ifmid,Long istartftid){
        if(ifmid == null || ifmid == 0){
            error(103,"ifmid未传");
            return resultMap;
        }
        try {
            FlowDirection flowDirection = new FlowDirection();
            flowDirection.setIstartftid(istartftid);
            flowDirection.setIfmid(ifmid);
            List<FlowDirection> list = flowDirectionService.find(flowDirection);
            if(list.size()>0){
                resultMap.put("data",list);
            }
            else {
                error(101,"未查询到相关数据");
            }
        }catch (Exception e){
            logger.error(e.toString(), e);
            error(102, "查询数据出现错误");
        }
        return resultMap;
    }
}
